package com.atguigu.springcloud.config;

import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Function;

/**
 * @author timiv
 */
@Configuration
public class MyRouteConfig {

    /**
     * 自定义路由配置
     * @param builder
     * @return
     */
    @Bean
    public RouteLocator myRoute(RouteLocatorBuilder builder){
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("guonei", new Function<PredicateSpec, Route.AsyncBuilder>() {
            @Override
            public Route.AsyncBuilder apply(PredicateSpec predicateSpec) {
                return predicateSpec.path("/guonei")
                        .uri("http://news.baidu.com/guonei");
            }
        });
        return routes.build();
    }

    /**
     * 自定义路由配置
     * @param builder
     * @return
     */
    @Bean
    public RouteLocator myRoute1(RouteLocatorBuilder builder){
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("guoji", r -> r.path("/guoji")
                .uri("http://news.baidu.com/guoji"));
        return routes.build();
    }
}
